简介
Nacos致力于帮助您发现、配置和管理微服务。Nacos提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。Nacos帮助您更敏捷和容易地构建、交付和管理微服务平台。Nacos是构建以“服务”为中心的现代应用架构 (例如微服务范式、云原生范式) 的服务基础设施。
在接下里的教程中,将使用Nacos作为微服务架构中的注册中心
- 1、Nacos 下载地址:https://github.com/alibaba/nacos/releases
- 2、执行如下命令启动Nacos:cmd startup.cmd -m standalone
- 3、在浏览器中打开Nacos提供的管理页面(用户名,密码为 nacos
可配置
)
Nacos 服务提供者
搭建工程
引入maven依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lutao</groupId>
<artifactId>nacos-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-client</name>
<description>Demo project for nacos</description>
<properties>
<java.version>1.8</java.version>
<nacos.version>0.2.1.RELEASE</nacos.version>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${nacos.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${nacos.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>application.properties中添加相关配置
1
2
3server.port=8070
spring.application.name=example
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848添加服务启动代码,并对外提供服务
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
public class NacosClientApplication {
public static void main(String[] args) {
SpringApplication.run(NacosClientApplication.class, args);
}
class EchoController {
"/echo/{string}", method = RequestMethod.GET) (value =
public String echo(@PathVariable String string) {
return "Hello Nacos Discovery " + string;
}
}
}启动服务提供者之后会在nacos的管理页面中看到nacos-provider相关的服务信息
Nacos 服务消费者
搭建工程
引入maven依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lutao</groupId>
<artifactId>nacos-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<nacos.version>0.2.1.RELEASE</nacos.version>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${nacos.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>application.properties中添加相关配置
1
2
3server.port=8080
spring.application.name=service-consumer
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848服务消费者可以根据服务名远程调用服务提供者提供的接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50package com.lutao.nacosconsumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
public class NacosConsumerApplication {
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
public class TestController {
private final RestTemplate restTemplate;
true) (required=
private DiscoveryClient discoveryClient;
public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}
"/echo/{str}", method = RequestMethod.GET) (value =
public String echo(@PathVariable String str) {
return restTemplate.getForObject("http://example/echo/" + str, String.class);
}
/**
* 获取所有服务
*/
"/services") (
public Object services() {
return discoveryClient.getInstances("example");
}
}
}
通过调用http://127.0.0.1:8080/echo/nacosclient
即可获取到服务提供者提供的数据信息
通过调用http://127.0.0.1:8080/services
可以看到Nacos中提供的相关的服务的信息,存储的信息很简单类似Eureka注册中心
Nacos 注意事项
兼容版本
SpringCloud | SpringBoot | Spring Cloud Alibaba |
---|---|---|
Greenwich | 2.1.x | 0.2.2(还没有RELEASE) |
Finchley | 2.0.x | 0.2.1 |
Edgware | 1.5.x | 0.1.1 |
Dalston | 1.5.x | 0.1.1 |
在SpringBoot 2.0.x以下时 Spring Cloud Alibaba为0.1.1 启动报错
1 | 2019/06/11 15:10:30 INFO [main] [org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer] : |
查看源码_如图报错
- NacosAutoServiceRegistration 继承 AbstractAutoServiceRegistration,AbstractAutoServiceRegistration的构造函数值接收一个ServiceRegistry serviceRegistry
解决问题
- 查看 AbstractAutoServiceRegistration 所在的依赖关系
- 限定 spring-cloud-Commons的版本号.当前的1.3.0 版本中AbstractAutoServiceRegistration的构造函数接收单个参数
通过dependencyManagement限制版本
1 | <dependencyManagement> |